package com.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectById;
import com.baomidou.mybatisplus.core.injector.methods.SelectPage;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdianda.constant.MessageConstant;
import com.youdianda.context.BaseContext;
import com.youdianda.dto.YddLinkPageDTO;
import com.youdianda.entity.YddCate;
import com.youdianda.entity.YddLink;
import com.youdianda.mapper.YddTagMapper;
import com.youdianda.result.PageResult;
import com.youdianda.result.Result;
import com.youdianda.service.YddLinkService;
import com.youdianda.mapper.YddLinkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author xliu
 * @description 针对表【ydd_link】的数据库操作Service实现
 * @createDate 2023-05-14 16:01:34
 */
@Service
public class YddLinkServiceImpl extends ServiceImpl<YddLinkMapper, YddLink>
        implements YddLinkService {

    /**
     * 链接详情
     *
     * @param id
     * @return
     */
    @Override
    public Result rowInfo(String id) {
        //非空判断
        if (id == null || id == "") {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        YddLink byId = getById(id);
        return Result.success(byId);
    }

    /**
     * 链接修改
     *
     * @param yddLink
     * @return
     */
    @Override
    @Transactional
    public Result update(YddLink yddLink) {
        if (yddLink.getTitle() == ""||yddLink.getTitle()==null) {
            return Result.error(MessageConstant.NOT_FOUND);
        }
        updateById(yddLink);
        return Result.success();
    }

    /**
     * 链接删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result del(String id) {
        //非空判断
        if (id == null || id == "") {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
         removeById(id);
        return Result.success();
    }

    /**
     * 链接添加
     *
     * @param yddLink
     * @return
     */
    @Override
    @Transactional
    public Result add(YddLink yddLink) {
        //查询数据是否已存在
        LambdaQueryWrapper<YddLink>lqw=new LambdaQueryWrapper<>();
        lqw.eq(YddLink::getTitle,yddLink.getTitle());
        lqw.eq(YddLink::getUrl,yddLink.getUrl());
        YddLink one = getOne(lqw);
        if (one!=null){
            return Result.error(MessageConstant.IS_FOUND);
        }
        yddLink.setDefault_data("1");
        save(yddLink);
        return Result.success();
    }

    /**
     * 链接列表分页
     *
     * @param dto
     * @return
     */
    @Override
    public Result list(YddLinkPageDTO dto) {
        if (dto.getPage() == null) {
            return Result.error(MessageConstant.NOT_FOUND);
        }
        if (dto.getLimit() == null) {
            return Result.error(MessageConstant.NOT_FOUND);
        }
        //创建IPage分页对象,设置分页参数
        IPage<YddLink> page = new Page<>(Integer.valueOf(dto.getPage()), Integer.valueOf(dto.getLimit()));
        //查询数据
        LambdaQueryWrapper<YddLink> like = Wrappers.<YddLink>lambdaQuery().like(StringUtils.hasLength(dto.getType()), YddLink::getType, dto.getType());
        //开启分页
        page(page, like);
        //封装
        PageResult pageResult = new PageResult();
        pageResult.setCount((int) page.getTotal());//总数
        pageResult.setTotalPages((int) page.getPages());//总页数
        pageResult.setPageSize((int) page.getSize());//每页页码
        pageResult.setCurrentPage((int) page.getCurrent());//当前页码
        pageResult.setData(page.getRecords());
        return Result.success(pageResult);
    }
}




